package pratice.learn.flow;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * @author 吴宇亮 on 2019/5/10 0010 下午 2:55
 */
public class FlowHelloWorld {

    /**
     * 创建Activiti流的相关的数据库表（注意:要保证本地有对应名字的数据库）
     */
    @Test
    public void creatTable(){
        ProcessEngine processEngine = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti/activiti.cfg.xml")
                .buildProcessEngine();
    }

    //得到流程引擎
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程
     */
    @Test
    public void deployProcess(){
        //得到流程部署的service
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().name("请假流程001")
                .addClasspathResource("activiti/template/HelloWorld.bpmn")
                .addClasspathResource("activiti/template/HelloWorld.png")
                .deploy();
        System.out.println("部署成功:流程部署ID: " + deploy.getId());
    }

    /**
     * 启动流程
     */
    @Test
    public void startProcess(){
        RuntimeService runtimeService = processEngine.getRuntimeService();

//        String id = "HelloWorld:1:2504";
//        runtimeService.startProcessInstanceById(id);

        String key = "HelloWorld";
        runtimeService.startProcessInstanceByKey(key);
        System.out.println("流程启动成功");
    }

    @Test
    public void findTask(){
        TaskService taskService = processEngine.getTaskService();
        String assignee = "王五";
        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
        for (Task task : list) {
            System.out.println(task);
        }
    }

    @Test
    public void completeTask(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        TaskService taskService = processEngine.getTaskService();

        String taskId = "10002";
        taskService.complete(taskId);
        System.out.println("任务完成");
    }


}

















